<template>
  <div>
    <el-dialog
      :title="this.$store.state.itemDialog.itemDialogTitle + '添加'"
      :visible.sync="isOpen">
      <el-form
        ref="form"
        :model="form">
        <el-form-item label="名称">
          <el-tooltip class="item" placement="top" effect="light" content="请勿添加重复项目">
            <el-input v-model="form.name"></el-input>
          </el-tooltip>
        </el-form-item>
        <div v-if="this.$store.state.itemDialog.itemDialogName == 'group'">
          <el-form-item label="简介">
            <el-input
              type="textarea"
              v-model="form.details">
            </el-input>
          </el-form-item>
        </div>
      </el-form>
      <div
        slot="footer"
        class="dialog-footer">
        <el-button @click="closeDialog">
          取 消
        </el-button>
        <el-button
          type="primary"
          @click="submitData">
          确 定
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { mapActions } from 'vuex'
export default {
  name: 'add-item-dialog',
  data () {
    return {
      title: '工作',
      dialogVisible: false,
      form: {
        name: '',
        details: ''
      }
    }
  },
  computed: {
    isOpen: {
      get () {
        return this.$store.state.itemDialog.isItemDialogOpen
      },
      set (value) {
        if (!value) {
          this.$store.commit('isAddItemDialogOpen', value)
        }
      }
    }
  },
  methods: {
    ...mapActions([
      'postDiaryData'
    ]),
    closeDialog () {
      this.isOpen = false
    },
    submitData () {
      if (this.postDiaryData(this.form)) {
        this.closeDialog()
      }
      this.$store.dispatch('getDiaryDataList', this.$store.state.itemDialog.itemDialogName)
    }
  }
}
</script>